package com.semidata.trp.decision.business;

import java.util.HashMap;
import java.util.Map;

import com.semidata.rtd.core.log.Logger;
import com.semidata.trp.message.business.Business;
import com.semidata.trp.decision.business.OrganizationForBudget;

public class BudgetData {

	// private Set<Integer> orgs;
	private Map<Integer, OrganizationForBudget> orgs = new HashMap<>();
	private Map<Integer, StrategyForBudget> strategies = new HashMap<>();

	
	public BudgetData() {
		
	}
	
	public BudgetData(Business.BudgetData data) {
	
//		Logger.log("INFO", "orgs size is : " + data.getOrganizationsList().size());
		for (Business.BudgetOrganization org : data.getOrganizationsList()) {
			orgs.put(
					org.getId(),
					new OrganizationForBudget(org.getId(), org.getIn(), org
							.getOut(), org.getAlly()));
		}
		// orgs = new HashSet<>(data.getOrganizationsList());
		for (Business.BudgetStrategy stra : data.getStrategiesList()) {
			strategies.put(stra.getId(), new StrategyForBudget(stra.getId(),
					stra.getIn(), stra.getOut(), stra.getAlly()));
		}
	}

	/*
	 * public boolean isOrgActive(int orgId) { return orgs.contains(orgId); }
	 */
	public boolean isOrgActive(int orgId, int mediaType, Resource resource) {
		if (!orgs.containsKey(orgId)) {
			Logger.log("DEBUG", "org id : " + orgId + " is not active");
			return false;
		}
		boolean flag = true;
		if (mediaType == 0) {
			if (orgs.get(orgId).getIn() == 0)
				flag = false;
			else
				flag = true;
		} else if (mediaType == 1) {
			if (orgs.get(orgId).getOut() == 0)
				flag = false;
			else
				flag = true;
		} else if (mediaType == 2) {
			if (orgs.get(orgId).getAlly() == 0)
				flag = false;
			else
				flag = true;
		} else {
			flag = false;
		}
		if (flag == false && !resource.isAllowShared())
			flag = true;
		return flag;
	}

	public boolean isStrategyActive(int strategyId, int mediaType) {
		if (!strategies.containsKey(strategyId)){
			Logger.log("DEBUG", "strategy id : " + strategyId + " is not active");
			return false;
		}
		if (mediaType == 0) {
			if (strategies.get(strategyId).getIn() == 0)
				return false;
			else
				return true;
		} else if (mediaType == 1) {
			if (strategies.get(strategyId).getOut() == 0)
				return false;
			else
				return true;
		} else if (mediaType == 2) {
			if (strategies.get(strategyId).getAlly() == 0)
				return false;
			else
				return true;
		} else {
			return false;
		}
		// return strategies.contains(strategyId);
	}

	public String toString() {

		StringBuffer sb = new StringBuffer();
		sb.append("orgs: \n");
		for (Map.Entry<Integer, OrganizationForBudget> entry : orgs.entrySet()) {
			sb.append("id:");
			sb.append(entry.getValue().getId());
			sb.append(",in:");
			sb.append(entry.getValue().getIn());
			sb.append(",out:");
			sb.append(entry.getValue().getOut());
			sb.append(",ally:");
			sb.append(entry.getValue().getAlly());
			sb.append("\n");
		}
		// sb.append(orgs.toString());
		// sb.append('\n');
		sb.append("strategies: ");
		for (Map.Entry<Integer, StrategyForBudget> entry : strategies
				.entrySet()) {
			sb.append("id:");
			sb.append(entry.getValue().getId());
			sb.append(",in:");
			sb.append(entry.getValue().getIn());
			sb.append(",out:");
			sb.append(entry.getValue().getOut());
			sb.append(",ally:");
			sb.append(entry.getValue().getAlly());
			sb.append("\n");
		}
		// sb.append(strategies.toString());
		// sb.append('\n');
		return sb.toString();

	}

}
